﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for AreaCountryInfoDB
/// </summary>
/// 
namespace CE.AreaCountry
{
    public class AreaCountryInfoDB
    {
        public AreaCountryInfoDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(AreaCountryInfo areacountryinfo)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocAreaCountryInfoInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter

            _sPara.Add("@AcId", areacountryinfo.AreaCountryId);
            _sPara.Add("@AreaId", areacountryinfo.AreaId);
            _sPara.Add("@CountryId", areacountryinfo.CountryId);
            _sPara.Add("@TAInsId", areacountryinfo.TaInsId);
            _sPara.Add("@PlanType", areacountryinfo.PlanType);

            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public AreaCountryInfo GetAreaCountryInfo(Int64 ArId, Int64 TaId, string PlanType)
        {
            AreaCountryInfo _areacountryinfo = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocAreaCountryInfoSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@AreaID", ArId);
            _spara.Add("@TAInsID", TaId);
            _spara.Add("@PlanType", PlanType);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _areacountryinfo = new AreaCountryInfo();
                    _areacountryinfo = FillDataRecord(_dt, 0);
                }
            }
            return _areacountryinfo;
        }

        public DataTable GetAreaCountryInfoList(Int64 ArId, Int64 TaId, string PlanType)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("AreaCountries");
            string _spName = "sprocAreaCountryInfoSelectItemList";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@AreaID", ArId);
            _sPara.Add("@TAInsID", TaId);
            _sPara.Add("@PlanType", PlanType);

            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

        private AreaCountryInfo FillDataRecord(DataTable myDataTable, int rowId)
        {
            AreaCountryInfo myAreaCountryInfo = new AreaCountryInfo();
            myAreaCountryInfo.AreaCountryId = Convert.ToInt64(myDataTable.Rows[rowId]["AreaCountryId"].ToString());
            myAreaCountryInfo.AreaId = Convert.ToInt64(myDataTable.Rows[rowId]["AreaId"].ToString());
            myAreaCountryInfo.CountryId = Convert.ToInt64(myDataTable.Rows[rowId]["CountryId"].ToString());

            return myAreaCountryInfo;
        }

        public Int64 GetAreaIDByCountryID(Int64 CoId)
        {
            Int64 AreaID = 0;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocAreaCountryInfoSelectSingleItemByCountry";

            SortedList _spara = new SortedList();
            _spara.Add("@CountryID", CoId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    AreaID = Convert.ToInt64(_dt.Rows[0]["AreaId"].ToString());
                }
            }
            return AreaID;


        }

        public void CheckAreaCountryItem(Int64 ArId, Int64 Taid, string PlanType)
        {
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocAreaCountryInfoCheckItem";

            SortedList _spara = new SortedList();
            _spara.Add("@AreaId", ArId);
            _spara.Add("@TAInsId", Taid);
            _spara.Add("@PlanType", PlanType);

            _db.ExecuteSP(_spName, _spara);

        }
    }
}